package com.kitty.game.equip.service;


import cn.hutool.core.util.RandomUtil;
import com.kitty.game.FieldValuePosConst;
import com.kitty.game.bag.message.RespIconCartoon;
import com.kitty.game.base.service.BagService;
import com.kitty.game.config.Equip;
import com.kitty.game.config.RoleDaofa;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.model.RoleEquipField;
import com.kitty.game.onlinemall.service.MallService;
import com.kitty.game.role.model.Role;
import com.kitty.mina.message.MessagePusher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Random;
import com.kitty.game.equip.service.EquipService;

@Service
public class FabaoService {
    @Autowired
    MallService mallService;
    @Autowired
    EquipService equipService;
    @Autowired
    BagService bagService;
    Logger logger = LoggerFactory.getLogger(FabaoService.class);

    /**
     * 掉落法宝
     */
    public RoleEquip getArtifact(Role role, String name, int xiangxin, int level) {
        short pos = bagService.getPos(role, false);
        if (pos <= 0) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("背包已满"));
            return null;
        }
        Equip equip = EquipDataPool.getByName(name);
        if (equip == null) {
            logger.error("法宝名称错误！={}=={}=={}=={}", role.getName(), name, xiangxin);
            return null;
        }
        RoleEquip roleEquip = new RoleEquip();
        roleEquip.setId(mallService.getRoleEquipId());
        roleEquip.setName(equip.getKey_name());
        roleEquip.setRoleId(role.getRoleId());
        roleEquip.setType("法宝");
        roleEquip.setPosition(pos);
        RoleEquipField roleEquipField = new RoleEquipField();
        roleEquipField.setType((short) 1);
        LinkedHashMap<Short, FiedValue> basicAttr = equipService.getArtifactBasicAttr(equip, roleEquip, "金色");
        roleEquipField.setField(basicAttr);
        roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);
        RoleEquipField roleEquipField1 = equipService.getBasicAttNew(equip);
        roleEquip.getFields().put(roleEquipField1.getType(), roleEquipField1);
        /**升级*/
        roleEquip.setEquipValue(FieldValuePosConst.LEVEL, level);
        RoleDaofa nextLevel = equipService.getDaofaData(roleEquip.queryLevel() + 1);
        /**道法归0*/
        roleEquip.setEquipValue(FieldValuePosConst.EXP, 0);
        /**设置下一等级的EXP*/
        roleEquip.setEquipValue(FieldValuePosConst.NEXT_LEVEL_EXP, nextLevel.getDaofa());
        {
            FiedValue fiedValue = roleEquip.getFields().get((short) 1).getField().get(FieldValuePosConst.ARTIFACT_POLAR);
            fiedValue.setValue((byte) xiangxin);
        }
        //roleEquip.addLimitTime(10);
        equipService.add(role, roleEquip);
        equipService.refreshRoleEquip(role, roleEquip);

        RespIconCartoon respIconCartoon = new RespIconCartoon();
        respIconCartoon.setName(roleEquip.getName());
        respIconCartoon.setParam(roleEquip.getId() + "");
        MessagePusher.pushMessage(role, respIconCartoon);
        logger.info("获得法宝==={}=={}=={}=={}", role.getName(), roleEquip.getName(), name, xiangxin);
        RespNotifyMiscEx respNotifyMiscEx = new RespNotifyMiscEx();
        respNotifyMiscEx.setMsg("你获得了一个#R" + name + "#n。");
        respNotifyMiscEx.setTime(new Long(System.currentTimeMillis() / 1000).intValue());
        MessagePusher.pushMessage(role, respNotifyMiscEx);
        return roleEquip;
    }

    /**
     * 掉落法宝
     */
    public RoleEquip getArtifact8(Role role, String name, int xiangxin) {
        return getArtifact(role, name, xiangxin, 8);
    }

    public RoleEquip getArtifact12(Role role, String name, int xiangxin) {
        return getArtifact(role, name, xiangxin, 12);
    }

    public RoleEquip getArtifact16(Role role, String name, int xiangxin) {
        return getArtifact(role, name, xiangxin, 16);
    }

    public RoleEquip getArtifact18(Role role, String name, int xiangxin) {
        return getArtifact(role, name, xiangxin, 18);
    }

    public RoleEquip getArtifact24(Role role, String name, int xiangxin) {
        return getArtifact(role, name, xiangxin, 24);
    }








    public void fabaoRandom(Role role, int level) {
        String[] fabaos = {"混元金斗", "番天印", "阴阳镜", "定海珠", "金蛟剪", "九龙神火罩", "卸甲金葫"};
        int fabaoIndex = new Random().nextInt(fabaos.length);
        String fabao = fabaos[fabaoIndex];
        int xiangxingIndex = RandomUtil.randomInt(1, 5);
        getArtifact(role, fabao, xiangxingIndex, level);
    }

}
